//
//  Gate.m
//  Tank For IPhone
//
//  Created by 奕 朱 on 11-2-25.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "GateView.h"

#import "Constant.h"
#import "Tank.h"
#import "Brick.h"
#import "MapLoader.h"


@implementation GateView
@synthesize tank;
@synthesize testString;

- (id) initWithCoder:(NSCoder *) coder {
	NSLog(@"initWithCoder start");
	
	if ((self = [super initWithCoder:coder])) {
		self.tank = [[Tank alloc] init];
		
		[MapLoader loadNextMap];
				
		//NSThread *thread1 = [[NSThread alloc] initWithTarget:self 
//												   selector:@selector(reAction) 
//													 object:nil];
//		
//		NSThread *thread2 = [[NSThread alloc] initWithTarget:self 
//												   selector:@selector(reAction1) 
//													 object:nil];
		
//		[self performSelectorOnMainThread:@selector(reAction) 
//						withObject:nil 
//							waitUntilDone:YES];
		
		[self performSelector:@selector(reAction) 
			withObject:nil 
				   afterDelay: .15f];
		
//		[thread1 start];
//		[thread2 start];

	}
	
	NSLog(@"initWithCoder end");
	return self;
}

- (id)initWithFrame:(CGRect)frame {
	NSLog(@"initWithFrame");

    
    self = [super initWithFrame:frame];
    if (self) {
		
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code.
}
*/

- (void) drawRect:(CGRect)rect {
	//NSLog(@"draw start");
	
	CGContextRef context = UIGraphicsGetCurrentContext();
	
	UIColor *redColor = [UIColor whiteColor];
	
	CGContextSetStrokeColorWithColor(context, redColor.CGColor);
	CGContextSetFillColorWithColor(context, redColor.CGColor);
	
	
	CGContextAddRect(context, CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT));

	CGContextDrawPath(context, kCGPathFillStroke);
	
	[tank draw:context];
	
	for (int i = 0; i < [tank.firedBulletQueue size]; i ++) {
		[[tank.firedBulletQueue getObject:i] draw:context];
	}
	
	Brick *brick = [[Brick alloc] init:10 y: 10];
	[brick draw:context]; 
	
}

- (void)dealloc {
    [super dealloc];
}




- (void) reAction {
	
	//NSLog(@"Gate View reAction");
	[self setNeedsDisplay];
	[self performSelector:@selector(reAction) 
			   withObject:nil 
			   afterDelay: .15f];
		
}

@end
